<template>
  <!-- 头部 -->
  <div class="box">
    <div class="top">
      商品类型搜索:
      <input
        class="top-input"
        v-model="form.className"
        placeholder="请输入内容"
        style="width: 250px; height: 40px; margin: 10px"
      />
      <el-button
        type="primary"
        icon="el-icon-search"
        style="height: 40px"
        @click="search"
        >搜索</el-button
      >
    </div>
    <!-- main主体 -->
    <div class="bottom">
      <el-button
        type="text"
        icon="el-icon-circle-plus-outline"
        size="small"
        class="main-input"
        @click="add"
        >新建</el-button
      >
      <!-- dialog 对话框 -->
      <el-dialog :title="title" :visible.sync="dialogFormVisible">
        <el-form :model="form" :rules="rules" ref="form">
          <el-form-item
            label="商品类型名称"
            :label-width="formLabelWidth"
            prop="className"
          >
            <el-input
              v-model="form.className"
              autocomplete="off"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="submit">确 定</el-button>
        </div>
      </el-dialog>
    </div>
    <!--table -->
    <el-table stripe style="width: 100%" :data="typeList">
      <el-table-column
        label="序号"
        width="280"
        prop="classId"
      ></el-table-column>
      <el-table-column
        label="商品类型名称"
        width="400"
        prop="className"
      ></el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-link
            target="_blank"
            class="text"
            :underline="false"
            @click="edit(scope)"
            >修改</el-link
          >
          <el-link
            type="danger"
            :underline="false"
            @click="removeFn(scope.row.classId)"
            >删除</el-link
          >
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { add, random, edit, remove, search } from '@/api/user'
export default {
  name: 'commodity-page',
  async created() {
    this.random()
  },
  data() {
    return {
      input: null,
      form: {
        className: '',
        classId: ''
      },
      title: '',
      typeList: null,
      classId: '',
      dialogFormVisible: false,
      formLabelWidth: '120px',
      rules: {
        className: [
          { required: true, message: '请输入商品类型', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在1-10个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async random() {
      const res = await random()
      console.log(res)
      this.typeList = res.currentPageRecords
      this.classId = res.currentPageRecords.classId
    },
    // add
    add() {
      this.dialogFormVisible = true
      this.title = '新增商品类型'
    },
    // 点击是新建还是添加
    async submit() {
      if (this.title === '修改商品类型') {
        const res = await edit(this.form.classId, this.form.className)
        console.log(res)
      } else {
        this.$refs.form.validateField(this.form)
        const res = await add(this.form)
        console.log(res)
      }
      this.random()
      this.dialogFormVisible = false
      this.$refs.form.resetFields()
    },
    // 修改
    async edit(scope) {
      this.dialogFormVisible = true
      this.title = '修改商品类型'
      this.form.className = scope.row.className
      this.form.classId = scope.row.classId
      // console.log(scoped)
    },
    // 删除
    async removeFn(classId) {
      try {
        await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        console.log(classId)
        await remove(classId)
        this.$message.success('删除成功')
        this.random()
      } catch (error) {
        console.log(error)
      }
    },
    // 搜索search
    async search() {
      const res = await search(this.form)
      console.log(res)
      this.typeList = res.currentPageRecords
      // this.random()
    }
  }
}
</script>

<style scoped lang="scss">
.box {
  background-color: #f4f6fa;
  height: 100vh;
  width: 100%;
  .top {
    height: 64px;
    width: 100%;
    background-color: #fff;
    margin: 25px 0;
    display: flex;
    align-items: center;
    padding: 0 17px;
    .top-input {
      border: 1px solid #f4f6fa;
      text-indent: 1em;
      border-radius: 5px;
      //   &::placeholder {
      //     position: relative;
      //     right: -10px;
      //   }
    }
  }
  .bottom {
    height: 100px;
    width: 100%;
    // padding: 0 15px;
    background-color: #fff;
    padding: 20px 15px 19px 17px;

    .main-input {
      width: 80px;
      height: 40px;
      background: #ee7941;
      color: #fff;
      border: 1px solid #ee7941;
    }
  }
  .text {
    margin-right: 20px;
  }
}
</style>
